package br.com.teste.ch01.ts;

//Pag  18, 19, 20, 21
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.Iterator;

import javax.xml.soap.Name;
import javax.xml.soap.Node;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPMessage;

public class DemoSoap {

	private static final String LocalName = "TimeRequest";
	private static final String Namespace = "http://ch01/mysoap";
	private static final String NamespacePrefix = "ms";

	private ByteArrayOutputStream out;
	private ByteArrayInputStream in;

	public static void main(String[] args) throws SOAPException, Exception {
		new DemoSoap().requisicao();
	}

	private void requisicao() throws SOAPException, Exception {
		SOAPMessage msg = criaSOAPMessage();
		SOAPEnvelope env = msg.getSOAPPart().getEnvelope();
		SOAPHeader hdr = env.getHeader();

		Name lookup_name = criaQname(msg);
		hdr.addHeaderElement(lookup_name).addTextNode("time_request");
		out = new ByteArrayOutputStream();
		msg.writeTo(out);
		trace("The sent SOAP message: ", msg);
		SOAPMessage response = processaRequisicao();
		extraiConteudoEImprime(response);

	}

	private void extraiConteudoEImprime(SOAPMessage msg) {
		try{
			SOAPBody body = msg.getSOAPBody();
			Name lookupName = criaQname(msg);
			Iterator it = body.getChildElements(lookup_name);
			Node next = (Node) it.next();
			String value = ()
		}
	}

	private SOAPMessage processaRequisicao() {
		process_incoming_soap();
		coordinate_streams();
		return criaSOAPMessage(in);
	}

	private SOAPMessage criaSOAPMessage(ByteArrayInputStream in2) {
		
		return null;
	}

	private void coordinate_streams() {
		in = new ByteArrayInputStream(out.toByteArray());
		out.reset();
	}

	private void process_incoming_soap() {
		try {
			coordinate_streams();
			SOAPMessage msg = criaSOAPMessage(in);
			Name lookup_name = criaQname(msg);
			SOAPHeader header = msg.getSOAPHeader();
			Iterator it = header.getChildElements(lookup_name);
			Node next = (Node) it.next();
			String value = (next == null) ? "Error!" : next.getValue();
			if (value.toLowerCase().contains("time_request")) {
				String now = new Date().toString();
				SOAPBody body = msg.getSOAPBody();
				body.addBodyElement(lookup_name).addTextNode(now);
				msg.saveChanges();
				msg.writeTo(out);
				trace("The received/processed SOAP message: ", msg);
			}
		} catch (SOAPException e) {
			System.err.println(e);
		} catch (IOException e) {
			System.err.println(e);
		}

	}

	private void trace(String string, SOAPMessage msg) {
		System.out.println("\n");
		System.out.println(string);
		try {
			msg.writeTo(System.out);
		} catch (SOAPException e) {
			System.err.println(e);
		} catch (IOException e) {
			System.err.println(e);
		}

	}

	private Name criaQname(SOAPMessage msg) {
		Name name = null;
		try {
			SOAPEnvelope env = msg.getSOAPPart().getEnvelope();
			name = env.createName(LocalName, NamespacePrefix, Namespace);
		} catch (SOAPException e) {
			System.err.println(e);
		}
		return name;
	}

	private SOAPMessage criaSOAPMessage() {
		// TODO Auto-generated method stub
		return null;
	}

}
